package com.ruoyi.web.controller.cs;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BasicsController;
import com.ruoyi.common.core.domain.Response;
import com.ruoyi.common.core.page.ResponsePageInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.cs.domain.TNotice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cs.domain.TMovable;
import com.ruoyi.cs.service.ITMovableService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 活动信息Controller
 * 
 * @author ruoyi
 * @date 2021-03-25
 */
@Api(tags = {"活动信息管理"})
@RestController
@RequestMapping("/cs/movable")
@SuppressWarnings("all")
public class TMovableController extends BasicsController
{
    @Autowired
    private ITMovableService tMovableService;

    /**
     * 后台查询活动信息列表
     */
    @PreAuthorize("@ss.hasPermi('cs:movable:list')")
    @ApiOperation("后台查询活动信息列表")
    @RequestMapping(name="后台查询活动信息列表",value="listBack",method = RequestMethod.POST)
    public ResponsePageInfo<TMovable> listBack(@RequestBody  TMovable tMovable)
    {
        startPage(tMovable.getPageNum(),tMovable.getPageSize());
        tMovable.setBackStage(Constants.BACKSTAGE);
        List<TMovable> list = tMovableService.selectTMovableList(tMovable);
        return toResponsePageInfo(list);
    }

    @PreAuthorize("@ss.hasPermi('cs:movable:selectTMovableById')")
    @ApiOperation("查询活动信息详情")
    @RequestMapping(name="查询活动信息详情",value="selectTMovableById",method = RequestMethod.GET)
    public Response<TMovable> selectTMovableById(String movableId)
    {
        TMovable tMovable = tMovableService.selectTMovableById(movableId);
        return Response.success(tMovable);
    }
    /**
     * 新增活动信息
     */
    @PreAuthorize("@ss.hasPermi('cs:movable:add')")
    @ApiOperation("新增活动信息")
    @RequestMapping(name="新增活动信息",value="add",method = RequestMethod.POST)
    @Log(title = "活动信息", businessType = BusinessType.INSERT)
    public Response<Integer> add(@RequestBody TMovable tMovable)
    {
        return toResponse(tMovableService.insertTMovable(tMovable));
    }

    /**
     * 修改活动信息
     */
    @PreAuthorize("@ss.hasPermi('cs:movable:edit')")
    @ApiOperation("修改活动信息")
    @RequestMapping(name="修改活动信息",value="edit",method = RequestMethod.POST)
    @Log(title = "活动信息", businessType = BusinessType.UPDATE)
    public Response<Integer> edit(@RequestBody TMovable tMovable)
    {
        return toResponse(tMovableService.updateTMovable(tMovable));
    }

    /**
     * 删除活动信息
     */
    @PreAuthorize("@ss.hasPermi('cs:movable:remove')")
    @ApiOperation("删除活动信息")
    @ApiImplicitParam(name="movableIds",value="单个或多个活动ID，用逗号拼接")
    @RequestMapping(name="删除活动信息",value="remove",method = RequestMethod.GET)
    @Log(title = "活动信息", businessType = BusinessType.DELETE)
    public Response<Integer> remove(String movableIds)
    {
        if (StringUtils.isNotEmpty(movableIds)) {
            String[] ids = movableIds.split(",");
            return toResponse(tMovableService.deleteTMovableByIds(ids));
        }else{
            return Response.fail(Constants.FAILCODE,"参数不能为空!");
        }
    }

    /**
     * 图片展示开关
     */
    @PreAuthorize("@ss.hasPermi('cs:movable:remove')")
    @ApiOperation("图片展示开关")
    @RequestMapping(name="图片展示开关",value="picDisplaySwitch",method = RequestMethod.GET)
    @Log(title = "活动信息", businessType = BusinessType.UPDATE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "picSwitch",value = "开关状态 0 开 1 关" ,dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "movableId",value = "活动ID" , dataType = "String", paramType = "query", required = true),
    })
    public Response<Integer> picDisplaySwitch(String picSwitch,String movableId)
    {
        if (StringUtils.isNotEmpty(movableId) && StringUtils.isNotEmpty(picSwitch)) {
            return toResponse(tMovableService.picDisplaySwitch(picSwitch, movableId));
        }else{
            return Response.fail(Constants.FAILCODE,"参数不能为空!");
        }
    }

    /**---------------------------------------前台接口-----------------------------------------*/
    /**
     * 查询活动信息列表
     */
    @ApiOperation("web查询活动信息列表")
    @RequestMapping(name="查询活动信息列表",value="common/list",method = RequestMethod.GET)
    public Response<List<TMovable>> list()
    {
        TMovable tMovable = new TMovable();
        tMovable.setDelFlag(Constants.SUCCESS);
        tMovable.setPicSwitch(Constants.SUCCESS);
        List<TMovable> list = tMovableService.selectTMovableList(tMovable);
        return Response.success(list);
    }

    @ApiOperation("web查询活动banner列表")
    @RequestMapping(name="查询活动信息列表",value="common/bannerList",method = RequestMethod.GET)
    @Cacheable(value="twoMinuteCache", key="'bannerList'")
    public Response<List<TMovable>> bannerList()
    {
        TMovable tMovable = new TMovable();
        tMovable.setDelFlag(Constants.SUCCESS);
        tMovable.setPicSwitch(Constants.SUCCESS);
        List<TMovable> list = tMovableService.selectTMovableList(tMovable);
        if (list.size() > 0){
            list.stream().forEach(bean -> {
                List<String> bannerPicList = new ArrayList<>();
                List<String> phoneBannerPicList = new ArrayList<>();
                if (StringUtils.isNotEmpty(bean.getBannerPic1())) {
                    bannerPicList.add(bean.getBannerPic1());
                }
                if (StringUtils.isNotEmpty(bean.getBannerPic2())) {
                    bannerPicList.add(bean.getBannerPic2());
                }
                if (StringUtils.isNotEmpty(bean.getBannerPic3())) {
                    bannerPicList.add(bean.getBannerPic3());
                }
                if (StringUtils.isNotEmpty(bean.getPhoneBannerPic1())) {
                    phoneBannerPicList.add(bean.getPhoneBannerPic1());
                }
                if (StringUtils.isNotEmpty(bean.getPhoneBannerPic2())) {
                    phoneBannerPicList.add(bean.getPhoneBannerPic2());
                }
                if (StringUtils.isNotEmpty(bean.getPhoneBannerPic3())) {
                    phoneBannerPicList.add(bean.getPhoneBannerPic3());
                }
                bean.setBannerPicList(bannerPicList);
                bean.setPhoneBannerPicList(phoneBannerPicList);
            });
        }
        return Response.success(list);
    }
}
